#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2010 - BidiX@BidiX.info
#
#
'''
    logger - Application log
    
    Description :
 
     Usage :
     
'''
from google.appengine.ext import db
from google.appengine.ext import webapp
from bidix.html import page

class Level():
    DEBUG=0
    INFO=1
    WARNING=2
    ERROR=3
    FATAL=4
    

class LogEntity(db.Model):
    #name always present
    created = db.DateTimeProperty(required=True, auto_now_add=True)
    user = db.UserProperty(auto_current_user_add=True)
    type = db.StringProperty()
    level = db.IntegerProperty()
    creator = db.StringProperty()
    url = db.TextProperty()
    source = db.TextProperty()
    id = db.TextProperty()
    
    

#    def __init__(self, type=None, level=Level.INFO, creator=None, url=None, source=None, id=None, *kwargs):
#        db.Model.__init__(self, log_type=type, level=level, creator=creator, url=url, source=source, id=id, **kwargs)
    
def query():
    q = db.Query(LogEntity, keys_only=True)
    return q
        
def log(type= None, level=Level.INFO, creator=None, url=None, source=None, id=None):
    l = LogEntity(type=type, level=level, creator=creator, url=url, source=source, id=id)
    l.put()
    return l
    
#class LogPage(webapp.RequestHandler):
class LogPage(page.Page):

    def initialize(self, request, response):
        page.Page.initialize(self, request, response)
        self.title = "Log"

    def _write_page(self):
        #self.out.write(self.__page_header)
        #if self.with_html_editor:
        #    self.out.write(self.__tiny_mce)
        #self.out.write(self.__page_content%{
        #            'page_menu': self.format_menu(),
        #            'page_main': self.format_main()
        #                                    })
        #self.out.write(self.__page_footer) 
        self.title = "Log"   
        self.out.write(self.format_main())
        
    def format_menu(self):
        return ""
            
    def  format_main(self):
        #results = list.fetch(list.count())
        results = self.query.fetch(100)
        response = "<div id='log'> <table class=\"borderedTable\">"
        response +="<tr>"
        response +="<th>created</th>"
        response +="<th>user</th>"
        response +="<th>type</th>"
        response +="<th>level</th>"
        response +="<th>creator</th>"
        response +="<th>url</th>"
        response +="<th>source</th>"
        response +="<th>id</th>"
        response +="</tr>"
        for l in results:
            response +="<tr>"
            #l = LogEntity.get(key)
            response +="<td>%s</td>"%l.created
            response +="<td>%s</td>"%l.user
            response +="<td>%s</td>"%l.type
            response +="<td>%s</td>"%l.level
            response +="<td>%s</td>"%l.creator
            response +="<td>%s</td>"%l.url
            response +="<td>%s</td>"%l.source
            response +="<td>%s</td>"%l.id
            response +="</tr>"
        response +="</table></div>"
        return response

    def get(self, *args):
        '''
        '''
        where_clause =  self.request.get('where')
        if where_clause:
            self.query = db.GqlQuery("select * from LogEntity where %s "%where_clause)
        else:
            self.query = db.Query(LogEntity)
            
        self._write_html()
             